<?php

/**
 * Class UserNewMsg_Model
 */
class UserNewMsg_Model extends CI_Model
{
    /**
     *
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $row
     * @return UserNewMsg
     */
    public static function getUserNewMsgByRow($row)
    {
        $userNewMsg = new UserNewMsg;
        $userNewMsg->setId($row->id);
        $userNewMsg->setUserId($row->user_id);
        $userNewMsg->setNewMsgCount($row->new_msg_count);
        $userNewMsg->setNewMsgIdJson($row->new_msg_id_json);
        return $userNewMsg;
    }

    /**
     * @param UserNewMsg $userNewMsg
     * @return array
     */
    public static function getModelByUserNewMsg($userNewMsg)
    {
        return array(
            'id' => $userNewMsg->getId(),
            'user_id' => $userNewMsg->getUserId(),
            'new_msg_count' => $userNewMsg->getNewMsgCount(),
            'new_msg_id_json' => $userNewMsg->getNewMsgIdJson(),
        );
    }

    /**
     * @param int $id
     * @return UserNewMsg
     */
    public static function getUserNewMsgById($id)
    {
        /** @var CI $CI */
        $CI =& get_instance();
        $CI->db->select('user_new_msg.*');
        $CI->db->from('user_new_msg');
        $CI->db->where('user_new_msg.id', $id);

        $query = $CI->db->get();
        $row = $query->row();

        if ($row) {
            $userNewMsg = self::getUserNewMsgByRow($row);
        } else {
            $userNewMsg = null;
        }
        return $userNewMsg;
    }

    /**
     * @param int $userId
     * @return UserNewMsg
     */
    public static function getUserNewMsgByUserId($userId)
    {
        if (!$userId) return new UserNewMsg();

        /** @var CI $CI */
        $CI =& get_instance();
        $CI->db->select('user_new_msg.*');
        $CI->db->from('user_new_msg');
        $CI->db->where('user_new_msg.user_id', $userId);

        $query = $CI->db->get();
        $row = $query->row();

        if ($row) {
            $userNewMsg = self::getUserNewMsgByRow($row);
        } else {
            $userNewMsg = new UserNewMsg;
            $userNewMsg->setUserId($userId);
            $userNewMsg->setNewMsgCount(0);
            $userNewMsg->setNewMsgIdJson(json_encode(array()));
            $userNewMsg = self::save($userNewMsg);
        }
        return $userNewMsg;
    }

    /**
     * @param UserNewMsg $userNewMsg
     * @throws DatabaseException
     * @return UserNewMsg
     */
    public static function save($userNewMsg)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        if (self::isUserIdExist($userNewMsg->getUserId())) {
            $CI->db->where('user_new_msg.user_id', $userNewMsg->getUserId());
            $result = $CI->db->update('user_new_msg', self::getModelByUserNewMsg($userNewMsg));
            if ($result)
                return self::getUserNewMsgById($userNewMsg->getId());
            else
                throw new DatabaseException('更新数据失败');
        } else {
            $result = $CI->db->insert('user_new_msg', self::getModelByUserNewMsg($userNewMsg));
            if ($result)
                return self::getUserNewMsgById($CI->db->insert_id());
            else
                throw new DatabaseException('插入数据失败');
        }
    }

    /**
     * @param int $userId
     * @return bool
     */
    public static function isUserIdExist($userId)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('user_new_msg.id');
        $CI->db->from('user_new_msg');
        $CI->db->where('user_new_msg.user_id', $userId);
        if ($CI->db->get()->num_rows())
            return true;
        else
            return false;
    }
}
